﻿var CalculatorHH = function (txtHoTenHH, txtSoNguoiLonHH, txtSoTreEmHH,
                txtGiaNguoiLonHH, txtGiaTreEmHH, txtVatHH, txtTongHH, seasonTour, tourInfo, txtGiaBabyHH, txtSoTBabyHH) {
    
    $(txtHoTenHH).val("");
    $(txtSoNguoiLonHH).val(0);
    $(txtSoTreEmHH).val(0);
    $(txtGiaNguoiLonHH).val(0);
    $(txtGiaTreEmHH).val(0);
    $(txtVatHH).val(0);
    $(txtTongHH).val(0);
    $(txtGiaBabyHH).val(0);
    $(txtSoTBabyHH).val(0);
    var funTongDvk = function () {
        var priceAdults = $(txtGiaNguoiLonHH).val();
        var priceChilds = $(txtGiaTreEmHH).val();
        var quantityAdults = $(txtSoNguoiLonHH).val();
        var quantityChild = $(txtSoTreEmHH).val();
        
        var priceBaby = $(txtGiaBabyHH).val();
        var quantityBaby = $(txtSoTBabyHH).val(); 
        var vat = $(txtVatHH).val();
        var total = 0;
        if (vat > 0 || vat < 100) {
            total = (parseInt(priceAdults) * parseInt(quantityAdults)) +
                (parseInt(priceChilds) * parseInt(quantityChild)) +
                (parseInt(priceBaby) * parseInt(quantityBaby));
            total = total + (total * vat / 100);
        }
        $(txtTongHH).val(total);
    };
    // goi load gia

    $(txtSoNguoiLonHH).keyup(funTongDvk);
    $(txtSoTreEmHH).keyup(funTongDvk);
    $(txtGiaNguoiLonHH).keyup(funTongDvk);
    $(txtGiaTreEmHH).keyup(funTongDvk);
    $(txtVatHH).keyup(funTongDvk);
    $(txtTongHH).keyup(funTongDvk);
    $(txtGiaBabyHH).keyup(funTongDvk);
    $(txtSoTBabyHH).keyup(funTongDvk);
    $('#btnInsertHH').live("click", function () {
       
        var typeId = 11;
        var tourId = parseInt(tourInfo); 
        var hoTen = $(txtHoTenHH).val();
        var priceAdults = $(txtGiaNguoiLonHH).val();
        var priceChilds = $(txtGiaTreEmHH).val();
        var quantityAdults = $(txtSoNguoiLonHH).val();
        var quantityChild = $(txtSoTreEmHH).val();
        var priceBaby = $(txtGiaBabyHH).val();
        var quantityBaby = $(txtSoTBabyHH).val();
        var vat = $(txtVatHH).val();
        if (vat > 0 && vat <= 100) { } else { vat = 0; }
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: {
                "function": "InsertHH", "TypeId": typeId, "TourId": tourId, "TravelGuideType": hoTen,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild,
                'QuantityBaby': quantityBaby, 'PriceBaby': priceBaby
            },
            dataType: "json",
            success: function (data) {

                $(".ListHH").remove();
                $(txtHoTenHH).val('');
                $(txtGiaNguoiLonHH).val('0');
                $(txtGiaTreEmHH).val('0');
                $(txtSoNguoiLonHH).val('0');
                $(txtSoTreEmHH).val('0');
                $(txtVatHH).val('0');
                $(txtTongHH).val('0');
                $(txtGiaBabyHH).val('0');
                $(txtSoTBabyHH).val('0');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) { 
                        var totaIitem = (parseInt(item.PriceAdults) * parseInt(item.QuantityAdults))
                            + (parseInt(item.PriceChild) * parseInt(item.QuantityChild)
                             + (parseInt(item.PriceBaby) * parseInt(item.QuantityBaby)));
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                      
                        $('#new-HH')
                            .before($("<tr class='ListHH' id='HH-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.TravelGuideType + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + totaIitem + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateHH"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteHH"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalHHPrices').html(data.TotalHHPrices);
                    $('#TotalHHAdults').html(data.TotalHHAdults);
                    $('#TotalHHChild').html(data.TotalHHChild);
                    $('#TotalHHBaby').html(data.TotalHHBaby);


                    $('#btnInsertHH').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteHH').show();
                    $('#btnDeleteHH').hide();
                }

            }
        });

    });
    $('.btnUpdateHH').live("click", function () {
        var calculatorId = $(this).attr("calculatorid");
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "UpdateHH", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {

                $('#HH-' + calculatorId).remove(); 
                $(txtHoTenHH).val(data.TravelGuideType);
                $(txtGiaNguoiLonHH).val(data.PriceAdults);
                $(txtGiaTreEmHH).val(data.PriceChild);
                $(txtSoNguoiLonHH).val(data.QuantityAdults);
                $(txtSoTreEmHH).val(data.QuantityChild); 
                $(txtGiaBabyHH).val(data.PriceBaby);
                $(txtSoTBabyHH).val(data.QuantityBaby);
                $(txtVatHH).val(data.Vat);
                $(txtTongHH).val(data.Total);
                $('#btnInsertHH').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateHH').hide();
                $('#btnDeleteHH').show();
            }
        });
    });
    $('.btnDeleteHH').live("click", function () {
        var typeId = 11;
        var tourId = parseInt(tourInfo);
        var hoTen = $(txtHoTenHH).val();
        var priceAdults = $(txtGiaNguoiLonHH).val();
        var priceChilds = $(txtGiaTreEmHH).val();
        var quantityAdults = $(txtSoNguoiLonHH).val();
        var quantityChild = $(txtSoTreEmHH).val();
        var priceBaby = $(txtGiaBabyHH).val();
        var quantityBaby = $(txtSoTBabyHH).val();
        var vat = $(txtVatHH).val();
        if (vat > 0 && vat <= 100) { } else { vat = 0; }
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: {
                "function": "InsertHH", "TypeId": typeId, "TourId": tourId, "TravelGuideType": hoTen,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild,
                'QuantityBaby': quantityBaby, 'PriceBaby': priceBaby
            },
            dataType: "json",
            success: function (data) {

                $(".ListHH").remove();
                $(txtHoTenHH).val('');
                $(txtGiaNguoiLonHH).val('0');
                $(txtGiaTreEmHH).val('0');
                $(txtSoNguoiLonHH).val('0');
                $(txtSoTreEmHH).val('0');
                $(txtVatHH).val('0');
                $(txtTongHH).val('0');
                $(txtGiaBabyHH).val('0');
                $(txtSoTBabyHH).val('0');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        var totaIitem = (parseInt(item.PriceAdults) * parseInt(item.QuantityAdults))
                            + (parseInt(item.PriceChild) * parseInt(item.QuantityChild)
                             + (parseInt(item.PriceBaby) * parseInt(item.QuantityBaby)));
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                        $('#new-HH')
                            .before($("<tr class='ListHH' id='HH-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.TravelGuideType + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + item.Total + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateHH"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteHH"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalHHPrices').html(data.TotalHHPrices);
                    $('#TotalHHAdults').html(data.TotalHHAdults);
                    $('#TotalHHChild').html(data.TotalHHChild);
                    $('#TotalHHBaby').html(data.TotalHHBaby);


                    $('#btnInsertHH').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteHH').show();
                    $('#btnDeleteHH').hide();
                }

            }
        });
    });

    var loadCityByService = function (nationalId, partnerId, cityId, provinceElement, partnerElement, locationTypeId, ddlLocationTypeDvk, LocationId, serviceId) {

        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "ListCityandConcernByNational", "NationalId": nationalId },
            dataType: "json",
            success: function (data) {
                $(provinceElement).html('<option value="-1"> --- Chọn tỉnh/thành phố--- </option>');
                $(partnerElement).html('<option value="-1"> --- Chọn nhà cung cấp--- </option>');
                if (data && data.isError == false) {
                    $.each(data.arrayCity, function (index, item) {
                        $(provinceElement)
                            .append($("<option></option>")
                                .attr("value", item.IDCity)
                                .text(item.NameCity));
                    });

                    $.each(data.arrayPartner, function (index, item) {
                        $(partnerElement)
                            .append($("<option></option>")
                                .attr("value", item.IDPartner)
                                .text(item.NamePartner));
                    });
                }
                $(ddlCityDvk).val(cityId);
                $(ddlPartnerDvk).val(partnerId);
                loadLocationByType(locationTypeId, ddlLocationTypeDvk, LocationId, serviceId);
            }
        });



    };
    var loadLocationByType = function (locationTypeId, ddlLocationTypeDvk, LocationId, serviceId) {

        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "ListLocationByLocationType", "LocationType": locationTypeId },
            dataType: "json",
            success: function (data) {
                $(ddlLocationDvk).html('<option value="-1"> ---Chọn địa điểm--- </option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlLocationDvk)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                }
                $(ddlLocationDvk).val(LocationId);
                loadByService(serviceId);
            }
        });
    };
    var loadByService = function (serviceId) {

        var typeId = 8;
        var partnerId = $(ddlPartnerDvk).val();
        var locationId = $(ddlLocationDvk).val();
        var season = seasonTour;
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "PriceListByDvk", "TypeId": typeId, "PartnerId": partnerId, "LocationId": locationId, "SeasonTour": season },
            dataType: "json",
            success: function (data) {
                $(ddlServiceDvk).html('<option value="-1">--- Chọn dịch vụ ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlServiceDvk)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.ServiceName));
                    });
                    $(ddlServiceDvk).val(serviceId);
                }

            }
        });
    };
};

